/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frm.billingAndPaymentProcessing.managedbean;

import commonInfrastructure.accountManagement.session.EmailSessionBean;
import frm.billingAndPaymentProcessing.entity.DunningEmailEntity;
import frm.billingAndPaymentProcessing.entity.InvoiceEntity;
import frm.billingAndPaymentProcessing.session.DunningEmailSessionBean;
import frm.billingAndPaymentProcessing.session.InvoiceSessionBean;
import frm.creditControl.entity.CreditAccountEntity;
import frm.creditControl.session.CreditControlSessionBean;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;

/**
 *
 * @author xhx
 */
@ManagedBean
@ViewScoped
public class ManageDunningEmailManagedBean implements Serializable{
private DunningEmailEntity dunningEmail;
private InvoiceEntity invoice;
private String receivedContent;
@EJB
EmailSessionBean emailSessionBean;
@EJB
InvoiceSessionBean invoiceSessionBean;
@EJB
DunningEmailSessionBean dunningEmailSessionBean;
@EJB
CreditControlSessionBean creditControlSessionBean;
    /** Creates a new instance of ManageDunningEmailManagedBean */
    public ManageDunningEmailManagedBean() {
    }
    
    public void initView(PhaseEvent event){
      if(dunningEmail == null){
            dunningEmail = (DunningEmailEntity) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("dunningEmail");
      } 
      if(invoice == null){
          invoice =  (InvoiceEntity)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("invoice");
      }
      if(receivedContent == null){
          receivedContent = FacesContext.getCurrentInstance().getExternalContext().getFlash().get("content").toString();
      }
    }
    
    public void sendDunningEmail(){
         String emailAddress = invoice.getDeliveryOrder().getParentSalesOrder().getCustomer().getEmail();
            
            
            emailSessionBean.sendDunningEmail(emailAddress,receivedContent,dunningEmail);
            
            List<DunningEmailEntity> existingDunnigEmails = getInvoice().getDunningEmails();
            existingDunnigEmails.add(getDunningEmail());
            invoice.setDunningEmails(existingDunnigEmails);
            dunningEmailSessionBean.createDunningEmail(dunningEmail);
            CreditAccountEntity creditAccount = invoice.getDeliveryOrder().getParentSalesOrder().getCustomer().getCreditAccount();
            creditAccount.setFrozen(true);
            creditControlSessionBean.saveChanges(creditAccount);
            invoiceSessionBean.saveChanges(getInvoice());
            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Dunning Email is Successfully sent to customer, and Customer Account is Frozen", ""));
    }
    public void back() throws IOException{
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("invoice", invoice);
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("dunningEmail", dunningEmail);
            FacesContext.getCurrentInstance().getExternalContext().redirect("commenceDunningEmail.xhtml");
    }

    public DunningEmailEntity getDunningEmail() {
        return dunningEmail;
    }

    public void setDunningEmail(DunningEmailEntity dunningEmail) {
        this.dunningEmail = dunningEmail;
    }

    public InvoiceEntity getInvoice() {
        return invoice;
    }

    public void setInvoice(InvoiceEntity invoice) {
        this.invoice = invoice;
    }

    public String getReceivedContent() {
        return receivedContent;
    }

    public void setReceivedContent(String receivedContent) {
        this.receivedContent = receivedContent;
    }
}
